﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ajayumi.Platform.Search
{
    /// <summary>
    /// SearchBase.cs
    /// Author   : 黄乙冬
    /// Date     : 2011-09-19
    /// </summary>
    public abstract class SearchBase
    {
        protected List<SearchItem> m_SearchItemList = new List<SearchItem>();

        /// <summary>
        /// 初始化查询项
        /// </summary>
        protected abstract void InitSearchItems();

        /// <summary>
        /// 获取用户，以供选择
        /// </summary>
        /// <returns></returns>
        protected static SearchOption[] GetUserOptions()
        {
            return null;
        }

        /// <summary>
        ///  重新创建选项，在创建高级查询的时候调用
        /// </summary>
        /// <returns></returns>
        public virtual List<SearchItem> CreateItems()
        {
            InitSearchItems();

            return m_SearchItemList;
        }

        /// <summary>
        /// 获取所有的查询项
        /// </summary>
        /// <returns></returns>
        public virtual List<SearchItem> GetAllItems()
        {
            return m_SearchItemList;
        }

        /// <summary>
        /// 获取查询项
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public SearchItem GetSearchItemByIndex(int index)
        {
            if (index < m_SearchItemList.Count)
            {
                return m_SearchItemList[index];
            }
            else
            {
                throw new ArgumentOutOfRangeException("index");
            }
        }

        
        /// <summary>
        /// 批量生成查询条件
        /// </summary>
        /// <param name="searchValues">查询项的值</param>
        /// <returns></returns>
        public string BuildSearchSql(IList<SearchItemValue> searchValues)
        {
            if (searchValues == null)
            {
                return string.Empty;
            }

            StringBuilder sbSql = new StringBuilder();

            bool isFirst = true;
            foreach (SearchItemValue sv in searchValues)
            {
                if (string.IsNullOrEmpty(sv.Value1))
                {
                    continue;
                }

                SearchItem item = GetSearchItemByIndex(sv.ItemIndex);
                item.Value = sv;
                string itemSql = item.ToSql();
                if (string.IsNullOrEmpty(itemSql))
                {
                    continue;
                }

                //第一个不要AND 和 OR
                if (!isFirst)
                {
                    if (item.IsOr)
                    {
                        sbSql.Append(" OR ");
                    }
                    else
                    {
                        sbSql.Append(" AND ");
                    }
                }

                sbSql.Append(itemSql);
                isFirst = false;
            }

            return sbSql.ToString();
        }

        /// <summary>
        /// 返回快速查询的条件
        /// </summary>
        /// <param name="value">快速查询的值</param>
        /// <returns></returns>
        public abstract string BuildQuickSearchSql(string value);

        /// <summary>
        /// 返回快速查询的关键字
        /// </summary>
        /// <returns></returns>
        public abstract string GetKeywordStr();

    }
}
